home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1996 February / macformat-034.iso / mac / Shareware City / Developers / simple-sockets-11-c / Simple Sockets 1.1 ƒ / Sample Apps / day_server / source / MacDayd.c < prev   
Encoding:
C/C++ Source or Header  |  1995-11-28  |  8.6 KB  |  462 lines  |  [TEXT/CWIE]

  1. /* 
  2.  * MacDayd.c
  3.  *
  4.  * Macintosh Interface for the Daytime Client.
  5.  *
  6.  * Mike Trent 9/94
  7.  *
  8.  */
  9. /* Added Universal Header #ifdefs. Removed dead code.
  10.  * MDT 11/27/95
  11.  */ 
  12.  
  13. // Determine if we are using universal headers.. 
  14. #ifdef UNIVERSAL_INTERFACES_VERSION
  15.     #define __UNIVERSAL_HEADERS__
  16. #endif
  17.  
  18.  
  19. #include <MacTCPCommonTypes.h>
  20.  
  21. #include <string.h>
  22. #include <stdio.h>
  23. #include <time.h>
  24. #include <stdlib.h>
  25.  
  26. #include "ip.h"
  27. #include "iperr.h"
  28.  
  29. /* defines */
  30. #define kBaseResID            128
  31. #define kMoveToFront        (WindowPtr) - 1L
  32. #define kUseDefaultProc        (void *)-1L
  33. #define kSleep                3600L
  34. #define kLeaveWhereItIs        false
  35.  
  36. #define mApple                128
  37. #define iAbout                1
  38.  
  39. #define mFile                129
  40. #define iQuit                1
  41.  
  42. #define mEdit                130
  43. /* dummy menu */
  44.  
  45. #define mConnection            131
  46. #define iServing            1
  47. #define iLocalNum            2
  48.  
  49. #define kIgnored            0
  50.  
  51. void InitToolBox(void);
  52. void MenuBarInit(void);
  53. void DoEvent (EventRecord *eventPtr);
  54. void HandleMouseDown (EventRecord *eventPtr);
  55. void HandleMenuChoice (long menuChoice);
  56. void HandleConnection(short item);
  57. void DoAbout(void);
  58. void DoGetTime(char *host, char *port, char *data);
  59. void DrawDefault(DialogPtr dialog);
  60. OSErr DoMySpin(void);
  61. void DoServe(char *port, char *data);
  62.  
  63. Boolean gDone = false;
  64.  
  65. main()
  66. {
  67.     EventRecord      event;
  68.         
  69.     InitToolBox();
  70.     MaxApplZone();
  71.     MoreMasters();MoreMasters();MoreMasters();MoreMasters();
  72.  
  73.     if (InitMacTCP() != noErr) {
  74.         SysBeep(3);
  75.         ExitToShell();
  76.     }
  77.     
  78.     MenuBarInit();
  79.     
  80.     gDone = false;
  81.     
  82.     while (gDone == false)
  83.     {
  84.         if (WaitNextEvent(everyEvent, &event, kSleep, nil))
  85.             DoEvent (&event);
  86.         else
  87.             ;//HandleNull();
  88.     }
  89.     DisposeMacTCP();
  90. }
  91.  
  92. void InitToolBox(void)
  93. {
  94. #ifdef __UNIVERSAL_HEADERS__
  95.     InitGraf(&qd.thePort);
  96. #else
  97.     InitGraf(&thePort);
  98. #endif // __UNIVERSAL_HEADERS__
  99.     InitFonts();
  100.     InitWindows();
  101.     InitMenus();
  102.     TEInit();
  103.     InitDialogs(nil);
  104.     InitCursor();
  105. }
  106.  
  107. void MenuBarInit (void)
  108. {
  109.     Handle            menuBar;
  110.     MenuHandle        menu;
  111.     
  112.     menuBar = GetNewMBar (kBaseResID);    
  113.     if (menuBar == nil)
  114.     {
  115.          SysBeep (3);
  116.          ExitToShell();
  117.      }
  118.     SetMenuBar (menuBar);
  119.     DisposHandle(menuBar);
  120.     
  121.     menu = GetMHandle(mApple);
  122.     AddResMenu (menu, 'DRVR');
  123.     
  124.     DrawMenuBar();
  125. }
  126.  
  127. void DoEvent (EventRecord *eventPtr)
  128. {
  129.     char    theChar;
  130.         
  131.     switch (eventPtr->what)
  132.     {
  133.         case mouseDown:
  134.             HandleMouseDown (eventPtr);
  135.             break;
  136.         case keyDown:
  137.         case autoKey:
  138.             theChar = eventPtr->message & charCodeMask;
  139.             if ((eventPtr->modifiers & cmdKey) != 0) 
  140.                 HandleMenuChoice (MenuKey (theChar));
  141.             break;
  142.     }
  143. }
  144.  
  145. /********** HandleMouseDown **********/
  146.  
  147. void HandleMouseDown (EventRecord *eventPtr)
  148. {
  149.     WindowPtr        whichWindow;
  150.     short            thePart;
  151.     long            menuChoice;
  152.     
  153.     thePart = FindWindow(eventPtr->where, &whichWindow);
  154.     
  155.     switch (thePart)
  156.     {
  157.         case inMenuBar:
  158.             menuChoice =  MenuSelect (eventPtr->where);
  159.             HandleMenuChoice(menuChoice);
  160.             break;
  161.         case inSysWindow:
  162.             SystemClick(eventPtr, whichWindow);
  163.             break;
  164.         case inDrag:
  165. #ifdef __UNIVERSAL_HEADERS__
  166.             DragWindow(whichWindow, eventPtr->where, &qd.screenBits.bounds);
  167. #else
  168.             DragWindow(whichWindow, eventPtr->where, &screenBits.bounds);
  169. #endif // __UNIVERSAL_HEADERS__
  170.             break;
  171.     }
  172. }
  173.  
  174.  
  175. /********** HandleMenuChoice **********/
  176.  
  177. void HandleMenuChoice (long menuChoice)
  178. {
  179.     short         menu;
  180.     short        item;
  181.     MenuHandle    appleMenu;
  182.     Str255        accName;
  183.     short        accNumber;
  184.  
  185.     
  186.     if (menuChoice != 0) 
  187.     {
  188.         menu = HiWord(menuChoice);
  189.         item = LoWord(menuChoice);
  190.         
  191.         switch (menu){
  192.         case mApple:
  193.             switch (item){
  194.             case iAbout:
  195.                 DoAbout();        
  196.                 break;
  197.             default:
  198.                 appleMenu = GetMHandle (mApple);
  199.                 GetItem (appleMenu, item, accName);
  200.                 accNumber = OpenDeskAcc(accName);
  201.                 break;
  202.             }
  203.             break;
  204.         case mFile:
  205.             if (item == iQuit) gDone = true;
  206.             break;
  207.         case mConnection:
  208.             HandleConnection(item);
  209.             break;
  210.         }
  211.         HiliteMenu (0);
  212.     }
  213. }
  214.  
  215. void HandleConnection(short item)
  216. {
  217.     char data[80], name[80], addr[16], port[10];
  218.     DialogPtr dialog;
  219.     short itemHit = 0;
  220.     short    t;
  221.     Handle    h;
  222.     Rect    r;
  223.     
  224.     switch (item) {
  225.     case iServing:
  226.         if ((GetHostName(&(name[1]), 0)) < 0) {
  227.             SysBeep(3);
  228.             return;
  229.         }
  230.         name[0] = (char)strlen(&(name[1]));            // Make it a pascal string
  231.  
  232.         /* ask which port # to serve on (13 default?) */
  233.         if ((dialog = GetNewDialog(128, nil, kMoveToFront)) == nil){
  234.             SysBeep(3);
  235.             return;
  236.         }
  237.         SetPort(dialog);
  238.         GetDItem(dialog,3,&t,&h,&r);
  239.         SetIText(h, (ConstStr255Param)name);    /* identify host's name/number */
  240.         DrawDefault(dialog);
  241.         
  242.         while ((itemHit != 1) && (itemHit != 2))
  243.             ModalDialog(nil, &itemHit);
  244.         if (itemHit == 2) {
  245.             DisposDialog(dialog);
  246.             return;
  247.         }
  248.         itemHit = 0;
  249.         
  250.         GetDItem(dialog,4,&t,&h,&r);
  251.         GetIText(h, (unsigned char *)port);
  252.         
  253.         DisposDialog(dialog);
  254.  
  255.         port[(port[0]+1)] = 0;            //C Terminate...
  256.         DoServe(&(port[1]), data);
  257.         
  258.         /* tell user a connection was made */
  259.         if ((dialog = GetNewDialog(129,nil, kMoveToFront)) == nil){
  260.             SysBeep(3);
  261.             return;
  262.         }
  263.         SetPort(dialog);
  264.         GetDItem(dialog,2,&t,&h,&r);
  265.         SetIText(h, (ConstStr255Param)data);
  266.         DrawDefault(dialog);
  267.         while (itemHit != 1)
  268.             ModalDialog(nil, &itemHit);
  269.         DisposDialog(dialog);    
  270.         break;
  271.     case iLocalNum:
  272.         if ((GetHostNameOnly(&(name[1]))) < 0) {
  273.             strcpy(name, (const char *)"\pNo DNS Name");
  274.         } else name[0] = (char)strlen(&(name[1]));            // Make it a pascal string
  275.         if ((GetMyIPDot(&(addr[1]))) < 0) {
  276.             SysBeep(3);
  277.             return;
  278.         }
  279.         addr[0] = (char)strlen(&(addr[1]));
  280.         
  281.         if ((dialog = GetNewDialog(130,nil, kMoveToFront)) == nil){
  282.             SysBeep(3);
  283.             return;
  284.         }
  285.         SetPort(dialog);
  286.         GetDItem(dialog,2,&t,&h,&r);
  287.         SetIText(h, (ConstStr255Param)addr);
  288.         GetDItem(dialog,3,&t,&h,&r);
  289.         SetIText(h, (ConstStr255Param)name);
  290.  
  291.         DrawDefault(dialog);
  292.         while (itemHit != 1)
  293.             ModalDialog(nil, &itemHit);
  294.         DisposDialog(dialog);
  295.         break;
  296.     }
  297. }
  298.  
  299. void DoServe(char *port, char *data)
  300. {
  301.     DialogPtr d;
  302.     struct sockaddr_in sa;
  303.     int s, m;
  304.     char *pts;
  305.     time_t now;
  306.     Boolean serving = true;
  307.     short    t;
  308.     Handle    h;
  309.     Rect    r;
  310.     char addr[80];
  311.  
  312.     m = socket(kIgnored, kIgnored,IPPROTO_TCP);
  313.     if (m < 0) {
  314.         sprintf(data, (const char *) "\psocket error");
  315.         return;
  316.     }
  317.     //sa.sin_family = AF_INET
  318.     //sa.sin_addr.s_addr = INADDR_ANY
  319.     sa.sin_port = atoi(port);
  320.     
  321.     if (bind (m,&sa, kIgnored)) {
  322.         sprintf(data,  (const char *) "\pbind error");
  323.         return;
  324.     }
  325.     
  326.     d = GetNewDialog(132, nil, (WindowPtr)-1L);
  327.     if (!d) {
  328.         sprintf(data, (const char *) "\pDLOG resource error");
  329.         return;
  330.     }
  331.     DrawDefault(d);
  332.     SetSpin(DoMySpin);
  333.     
  334.     while (1) { /* jury rigged infinite loop */
  335.         if ((s = accept(m, &sa,kIgnored)) == -1) {
  336.             sprintf(data,  (const char *)"\paccept error");
  337.             DisposeDialog(d);
  338.             return; 
  339.         }
  340.     
  341.         (void) time(&now);
  342.         pts = ctime(&now);
  343.         (void) write(s, pts, strlen(pts));
  344.         strcpy(&(data[1]), pts); data[0] = (strlen(&(data[1]) -1));
  345.     
  346.         (void) close(s);
  347.         
  348.         num2dot(sa.sin_addr, (char *)&addr[1]);
  349.         addr[0] = strlen((char*)&addr[1]);
  350.         sprintf(port, (const char *)"\p%d", sa.sin_port);
  351.         
  352.         GetDItem(d,2,&t,&h,&r);
  353.         SetIText(h, (ConstStr255Param)addr);
  354.         GetDItem(d,3,&t,&h,&r);
  355.         SetIText(h, (ConstStr255Param)port);
  356.         GetDItem(d,4,&t,&h,&r);
  357.         SetIText(h, (ConstStr255Param)data);
  358.     }
  359.     return;    
  360. }
  361.  
  362.  
  363.  
  364. void DoAbout(void)
  365. {
  366.     short itemHit = 0;
  367.     DialogPtr dialog;
  368.     
  369.     dialog = GetNewDialog(131, nil, kMoveToFront);
  370.     if (dialog == 0L){
  371.         SysBeep(3);
  372.         ExitToShell();
  373.     }
  374.     SetPort(dialog);
  375.     DrawDefault(dialog);
  376.     while (itemHit != 1)
  377.         ModalDialog(nil, &itemHit);
  378.     DisposDialog(dialog);
  379.     
  380.     return;
  381. }
  382.  
  383. /********** DrawDefault **********/
  384.  
  385. void DrawDefault(DialogPtr dialog)
  386. {
  387.     short    type;
  388.     Handle    dummy;
  389.     Rect    button;
  390.  
  391.     SetPort(dialog);
  392.     GetDItem(dialog,1,&type,&dummy,&button);
  393.     PenSize(3,3);
  394.     InsetRect(&button, -4,-4);
  395.     FrameRoundRect(&button,16,16);
  396.     
  397.     return;
  398. }
  399.  
  400. OSErr DoMySpin(void)
  401. {
  402.     EventRecord event;
  403.     DialogPtr    d;
  404.     short item, thePart;
  405.     Boolean        eventp;
  406.     WindowPtr whichWindow;
  407.     
  408.     eventp = WaitNextEvent(everyEvent, &event, 30L, nil);
  409.     if (IsDialogEvent(&event)) {
  410.         if (DialogSelect(&event, &d, &item)) {
  411.             if (item == 1) {
  412.                 DisposeMacTCP();
  413.                 ExitToShell();
  414.             }
  415.         }
  416.     } 
  417.     if (eventp) {
  418.         switch (event.what)
  419.         {
  420.         case mouseDown:
  421.             thePart = FindWindow(event.where, &whichWindow);
  422.             
  423.             switch (thePart)
  424.             {
  425.                 case inSysWindow:
  426.                     SystemClick(&event, whichWindow);
  427.                     break;
  428.                 case inDrag:
  429. #ifdef __UNIVERSAL_HEADERS__
  430.                     DragWindow(whichWindow, event.where, &qd.screenBits.bounds);
  431. #else
  432.                     DragWindow(whichWindow, event.where, &screenBits.bounds);
  433. #endif // __UNIVERSAL_HEADERS__
  434.                     break;
  435.             }
  436.             break;
  437.         case keyDown:
  438.         case autoKey:
  439.             switch(event.message & charCodeMask) {
  440.             case 0x0d:    /* ret */
  441.             case 0x03:    /* enter */
  442.                 /* blink the button someday ... */
  443.                 DisposeMacTCP();
  444.                 ExitToShell();
  445.                 break;
  446.             }
  447.             break;
  448.         case osEvt:
  449.             if ((event.message & suspendResumeMessage) == resumeFlag) {
  450.                 /* resume */
  451.                 DrawDefault(FrontWindow());
  452.             } else
  453.                 /* suspend */
  454.                 ;
  455.             break;
  456.         default: break;
  457.         } /* switch */
  458.     } else
  459.         ;//HandleNull();
  460.     return 0;
  461. }
  462.